<?php
/* 	
	PHP 5 Script Filename: class.dictionary.php
	Author: Andrey Sumtsov
	Wrote in: Saint-Petersburg
	Begin: 15.07.2007
	Version: 1.7.5	Version Date: 08.08.2007
	Require: None
*/

class Dictionary {
	private $lang, $dicFile, $avaible_codes, $languages, $cfgArray;
	
	function __construct() {
	 $cfgArray = array(
	  'file' => false,
	  'setlng' => false,
  );
	}
	
	function version() {
  return 175;
 }
 
 function makeConfig($dicFile, $langCode) {
  $this->loadLanguages($dicFile);
  $this->setLang($langCode);
  if (!$this->getConfigState()) return false;
  else return true;
 }
 
 function getConfigState() {
  foreach($this->cfgArray as $step) {
   if (!$step)
    return false;
  }
  return true;
 }
 
 // set up config from pointed file
 function makeConfigFromFile($fileName) {
		if (!file_exists($fileName)) {
   echo("Configuration file is not found on pointed address `<i>$fileName</i>`.");
   return false;
  }
	 $config = parse_ini_file($fileName);
	 if (!($dic = @$config['dictionary_file'])) {
   echo("Configuration failed: unable to find necessary variable `<i>dictionary_file</i>`.");
   return false;
  }
	 if (!($l_code = @$config['lang_code'])) {
   echo("Configuration failed: unable to find necessary variable `<i>lang_code</i>`.");
   return false;
  }
  if ($this->makeConfig($dic, $l_code))
   return true;
  else {
   echo("Unable to make configuration with specified file `<i>$fileName</i>`.");
   return false;
  }
 }

 function getDicTarget ($dicFile) {
  if(file_exists($dicFile)) {
   $handle = fopen($dicFile, "r");
   while (!feof($handle)) {
    $line = fgets($handle, 4096);
    if (strpos($line,'; ID:') !== false) {
     fclose($handle);
     $id = sscanf($line,"; ID:%s");
     $id = $id[0];
     return $id;
    }
   }
   fclose($handle);
   return false;
  } else printf('Language file `%s` is not found.',$dicFile);
 }
 
 function getDicFilename() {
  return $this->dicFile;
 }
 
	function loadLanguages($dicFile) {
		if(file_exists($dicFile)) {
			$this->languages = parse_ini_file($dicFile, true);
			if (array_key_exists('FULL_LANG',$this->languages) === false) {
    echo("Ini file `<i>$dicFile</i>` isn`t match to essential structure: there must be FULL_LANG section with full languages names written on its own language.");
    $this->cfgArray['file'] = false;
    return false;
   }
			$this->avaible_codes = array_keys(array_change_key_case($this->languages['FULL_LANG']));
			$this->cfgArray['file'] = true;
			$this->dicFile = $dicFile;
			return true;
		} else {
			printf("Language file `%s` is not found.",$dicFile);
			$this->cfgArray['file'] = false;
   return false;
		}
	}
	
	function say($const) {
		if(!$this->getConfigState()) die("You must configure `<i>".__CLASS__."</i>` class via an `<i>makeConfig</i>` or `<i>setConfigFromFile</i>` methods befor using `<i>".__METHOD__."</i>` method.");
		if(!array_key_exists($const,$this->languages)) {
			return "Translation failed for $const constant.";
		}
		if ((func_num_args() > 1) and (substr_count($this->languages[$const][$this->lang], '%') == func_num_args()-1)) {
			$args = func_get_args();
			array_shift($args);
			return vsprintf(strval($this->languages[$const][$this->lang]),$args);
		} else return $this->languages[$const][$this->lang];
	}
	
	function setLang($lang) {
		if(is_null($this->dicFile)) {
   ('You must specify .ini file with laguage sets with method <i>loadLanguages</i>.');
   $this->cfgArray['setlng'] = false;
   return false;
  }
  $this->cfgArray['setlng'] = false;
  if (is_null($lang)) {
 		$codes = implode('|',$this->avaible_codes);
   preg_match("'\/($codes)\/'",$_SERVER['SCRIPT_FILENAME'],$lang_code);
   if (count($lang_code)>1) {
    $this->lang = $lang_code[1];
    $this->cfgArray['setlng'] = true;
    return true;
   } else {
 			echo('Unable to read language code from URL.');
 			return false;
   }
  } else {
 		if (($lang == '') or (strlen($lang) != 2)) {
 			echo('You must specify language code according with ISO 639-1 format. For example, `<i>en</i>` &mdash; english, `<i>ru</i>` &mdash; russian. Trying to set default language: en.');
 			return false;
 		} else {
 			if (in_array($lang, $this->avaible_codes)) {
 				$this->lang = $lang;
 				$this->cfgArray['setlng'] = true;
 				return true;
 			} elseif (in_array('en', $this->avaible_codes)) {
  			$this->lang = 'en';
     $this->cfgArray['setlng'] = true;
     return false;
  		} else {
     echo('There is no essential english language in ini file.');
     return false;
 			}
 		}
 	}
	}
	
	function setConstants() {
		if(!$this->getConfigState()) die("You must configure `<i>".__CLASS__."</i>` class via an `<i>makeConfig</i>` or `<i>setConfigFromFile</i>` methods befor using `<i>".__METHOD__."</i>` method.");
		$count = 0;
		foreach ($this->languages as $const => $val)
			if(!defined($const)) {
				define($const,$val[$this->lang]);
				$count++;
			} else {
			 $this->say('ER_CONST_REDEF');
				exit;
			}
		return $count;
	}
}